package com.xnyzc.lhy.common.entity;


import com.alibaba.fastjson.JSON;
import com.xnyzc.lhy.common.penum.worksheet.WorksheetErrorCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * 工单 接口返回样式
 */
@Data
@ApiModel(value = "返回实体", description = "返回实体")
public class WorksheetRv<T> implements Serializable {

    //错误码
    @ApiModelProperty(value = "错误码，SUCCESS ： 成功 OTHER： 错误", example = "0")
    private int errno;

    //返回msg
    @ApiModelProperty(value = "状态描述")
    private String errmsg;

    //返回数据
    @ApiModelProperty(value = "返回数据")
    private T data;

    @ApiModelProperty(value = "总数据数")
    private Integer pageTotal;

    private WorksheetRv() {
        pageTotal = 0;
    }

    /**
     * 获取成功实例
     *
     * @return
     */
    public static <T> WorksheetRv wrapSuccess(T data) {
        WorksheetRv rv = new WorksheetRv();
        rv.setErrno(WorksheetErrorCode.success.getValue());
        rv.setErrmsg(WorksheetErrorCode.success.toChString());
        rv.setData(data);
        return rv;
    }

    /**
     * 获取成功实例
     *
     * @return
     */
    public static <T> WorksheetRv wrapSuccess() {
        WorksheetRv rv = new WorksheetRv();
        rv.setErrno(WorksheetErrorCode.success.getValue());
        rv.setErrmsg(WorksheetErrorCode.success.toChString());
        return rv;
    }

    /**
     * 获取失败实例
     *
     * @return
     */
    public static <T> WorksheetRv wrapError(WorksheetErrorCode code) {
        WorksheetRv rv = new WorksheetRv();
        rv.setErrno(code.getValue());
        rv.setErrmsg(code.getDesc());
        return rv;
    }


    /**
     * 获取成功实例
     *
     * @param code
     * @return
     */
    public static <T> WorksheetRv wrapErrorCode(WorksheetErrorCode code) {
        WorksheetRv rv = new WorksheetRv();
        rv.setErrno(code.getValue());
        rv.setErrmsg(code.getDesc());
        rv.setData(new Object());
        return rv;
    }

    /**
     * 获取成功实例
     *
     * @param code
     * @return
     */
    public static <T> WorksheetRv wrapErrorCodeArray(WorksheetErrorCode code) {
        WorksheetRv rv = new WorksheetRv();
        rv.setErrno(code.getValue());
        rv.setErrmsg(code.getDesc());
        rv.setData(new ArrayList<>());
        return rv;
    }

    /**
     * 获取成功实例
     *
     * @param code
     * @return
     */
    public static <T> WorksheetRv wrapErrorCodeArray(WorksheetErrorCode code,ArrayList data) {
        WorksheetRv rv = new WorksheetRv();
        rv.setErrno(code.getValue());
        rv.setErrmsg(code.getDesc());
        rv.setData(data);
        return rv;
    }

    /**
     * 获取成功实例
     *
     * @param data
     * @return
     */
    public static <T> WorksheetRv wrapSuccessCode(T data) {
        WorksheetRv rv = new WorksheetRv();
        rv.setErrno(WorksheetErrorCode.success.getValue());
        rv.setErrmsg(WorksheetErrorCode.success.toChString());
        rv.setData(new Object());
        return rv;
    }

    /**
     * 获取实例
     *
     * @param data
     * @return
     */
    public static <T> WorksheetRv wrap(WorksheetErrorCode code, T data) {
        WorksheetRv rv = new WorksheetRv();
        rv.setErrno(code.getValue());
        rv.setErrmsg(code.getDesc());
        rv.setData(data);
        return rv;
    }

    /**
     * 合力获取订单列表专用
     *
     * @param data
     * @return
     */
    public static <T> WorksheetRv wrapHeliGetOrderDetail(WorksheetErrorCode code, T data, Integer pageTotal) {
        WorksheetRv rv = new WorksheetRv();
        rv.setErrno(code.getValue());
        rv.setErrmsg(code.getDesc());
        rv.setPageTotal(pageTotal);
        rv.setData(data);
        return rv;
    }

    /**
     * 获取实例
     *
     * @param msg
     * @param data
     * @return
     */
    public static <T> WorksheetRv wrap(WorksheetErrorCode code, String msg, T data) {
        WorksheetRv rv = new WorksheetRv();

        rv.setErrno(code.getValue());
        rv.setErrmsg(msg != null ? msg : code.getDesc());
        rv.setData(data);
        return rv;
    }

    public String toString() {
        return JSON.toJSONString(this);
    }
}
